package com.ioidea.base.widget

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.ioidea.base.PreviewPhoneSize
import com.ioidea.base.theme.AppTheme
import com.ioidea.base.theme.dimensions.phoneDimensions


/**
 * @author: hemin
 * @date: 2024/4/16
 * @Desc:
 */
@Composable
fun LoadingDialog(
    message: String = "Loading...",
    showDialog: Boolean,
    bgSize: Int = 160,
    circularSize: Int = 60,
    onDismissRequest: () -> Unit = {}
) {
    if (showDialog) {
        Dialog(
            onDismissRequest = onDismissRequest,
            properties = DialogProperties(
                dismissOnBackPress = false,
                dismissOnClickOutside = false
            ),
        ) {
            // 使用Card作为对话框的容器
            Card(
                colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.onPrimaryContainer),
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,
                    modifier = Modifier.size(bgSize.dp)
                ) {
                    CircularProgressIndicator(
                        modifier = Modifier.size(circularSize.dp),
                        color = MaterialTheme.colorScheme.onPrimary,
                        strokeWidth =4.0.dp,
                    )
                    Spacer(Modifier.height(phoneDimensions.hugePadding))  // 添加间隔
                    Text(text = message, color = MaterialTheme.colorScheme.onPrimary)  // 显示传递的消息
                }
            }
        }
    }
}

@Preview(showBackground = true, device = PreviewPhoneSize.PreviewPhoneSize)
@Composable
fun PreViewShowLoadingDialogMyScreen() {
    AppTheme {
        var showDialog by remember { mutableStateOf(true) }

        LoadingDialog(
            message = "正在加载...",
            showDialog = showDialog,
            onDismissRequest = {

            }
        )
    }
}